using Microsoft.AspNetCore.Mvc;
using RuoVea.ExJwtBearer;

namespace RuoVea.WeighIns.Server.Service;

/// <summary>
/// 用户设置服务
/// </summary>
[ApiExplorerSettings(GroupName = "WeighInfo")]
public partial class UserSettingsService : IApplicationService
{
    private readonly SugarRepository<InsUserSettings> _thisRepository;
    private readonly ICurrentUser _currentUser;
    public UserSettingsService(SugarRepository<InsUserSettings> thisRepository, ICurrentUser currentUser)
    {
        _thisRepository = thisRepository;
        _currentUser = currentUser;
    }

    /// <summary>
    /// 获取用户设置
    /// </summary>
    public async Task<InsUserSettings> GetUserSettingsAsync()
    {
        return await GetUserSettingsAsync(_currentUser.UserId);
    }

    /// <summary>
    /// 获取用户设置
    /// </summary>
    public async Task<InsUserSettings> GetUserSettingsAsync(long userId)
    {
        var settings = await _thisRepository.GetFirstAsync(u => u.UserId == userId);
        if (settings == null)
        {
            settings = new InsUserSettings
            {
                UserId = userId,
            };
            await _thisRepository.InsertAsync(settings);
        }
        return settings;
    }

    /// <summary>
    /// 更新用户设置
    /// </summary>
    public async Task<bool> UpdateUserSettingsAsync(UserSettingsDto dto)
    {
        return await UpdateUserSettingsAsync(_currentUser.UserId, dto);
    }

    /// <summary>
    /// 更新用户设置
    /// </summary>
    public async Task<bool> UpdateUserSettingsAsync(long userId, UserSettingsDto dto)
    {
        var settings = await _thisRepository.GetFirstAsync(u => u.UserId == userId);
        if (settings == null)
        {
            settings = new InsUserSettings
            {
                UserId = userId,
            };
            await _thisRepository.InsertAsync(settings);
        }

        // 更新基础信息
        settings.Height = dto.Height;
        settings.Gender = dto.Gender;
        settings.BirthDate = dto.BirthDate;

        // 更新其他设置
        settings.UnitSystem = dto.UnitSystem;
        settings.HealthGoal = dto.HealthGoal;
        settings.WeeklyGoal = dto.WeeklyGoal;
        settings.ActivityLevel = dto.ActivityLevel;
        settings.ReminderTime = dto.ReminderTime;
        settings.EnableReminder = dto.EnableReminder;
        settings.ReceiveWeeklyReport = dto.ReceiveWeeklyReport;
        settings.ReceiveTrendAlerts = dto.ReceiveTrendAlerts;
        settings.DataSharing = dto.DataSharing;

        return await _thisRepository.UpdateAsync(settings);
    }

    /// <summary>
    /// 计算基础代谢率(BMR)
    /// </summary>
    public double CalculateBMR(InsUserSettings settings, double weight, double height, int age, int gender)
    {
        // gender: 1-男, 2-女
        return gender == 1
            ? 10 * weight + 6.25 * height - 5 * age + 5  // 男性
            : 10 * weight + 6.25 * height - 5 * age - 161; // 女性
    }

    /// <summary>
    /// 计算每日总能量消耗(TDEE)
    /// </summary>
    public double CalculateTDEE(double bmr, int activityLevel)
    {
        var multiplier = activityLevel switch
        {
            1 => 1.2,   // 久坐
            2 => 1.375,  // 轻度活动
            3 => 1.55,   // 中度活动  
            4 => 1.725,  // 高度活动
            5 => 1.9,    // 运动员
            _ => 1.2
        };
        return bmr * multiplier;
    }
}
